# Bazel build
# C/C++ documentation: https://docs.bazel.build/versions/master/be/c-cpp.html

# ************************** IMPORTANT ***********************
# If a target or file is not found here, then please try searching in /src
# We are in the middle of transitioning to a tree of build files.
# If you would like to help with the move in your PR, please use `git mv` so that the history of the file is retained.

load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_cc_library")
load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library")
load("@com_github_grpc_grpc//bazel:cython_library.bzl", "pyx_library")
load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")
load("@python3_9//:defs.bzl", python39 = "interpreter")
load("@rules_cc//cc:defs.bzl", "cc_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_python//python:defs.bzl", "py_library", "py_runtime", "py_runtime_pair")
load("//bazel:ray.bzl", "COPTS", "FLATC_ARGS", "PYX_COPTS", "PYX_SRCS", "copy_to_workspace", "ray_cc_binary", "ray_cc_library", "ray_cc_test")

package(
    default_visibility = ["//visibility:public"],
)

# Hermetic python environment, currently only used for CI infra and scripts.

py_runtime(
    name = "python3_runtime",
    interpreter = python39,
    python_version = "PY3",
    visibility = ["//visibility:private"],
)

py_runtime_pair(
    name = "python_runtime_pair",
    py2_runtime = None,
    py3_runtime = ":python3_runtime",
    visibility = ["//visibility:private"],
)

constraint_setting(name = "hermetic")

constraint_value(
    name = "hermetic_python",
    constraint_setting = ":hermetic",
)

toolchain(
    name = "python_toolchain",
    exec_compatible_with = [":hermetic_python"],
    toolchain = ":python_runtime_pair",
    toolchain_type = "@bazel_tools//tools/python:toolchain_type",
)

platform(
    name = "hermetic_python_platform",
    constraint_values = [":hermetic_python"],
    parents = ["@local_config_platform//:host"],
    visibility = ["//visibility:private"],
)

# C/C++ toolchain constraint configs.

config_setting(
    name = "msvc-cl",
    flag_values = {"@bazel_tools//tools/cpp:compiler": "msvc-cl"},
)

config_setting(
    name = "clang-cl",
    flag_values = {"@bazel_tools//tools/cpp:compiler": "clang-cl"},
)

config_setting(
    name = "opt",
    values = {"compilation_mode": "opt"},
)

bool_flag(
    name = "jemalloc_flag",
    build_setting_default = True,
)

config_setting(
    name = "jemalloc",
    constraint_values = ["@platforms//os:linux"],
    flag_values = {":jemalloc_flag": "true"},
)

# bazel run :refresh_compile_commands for compile_commands generation for clangd
# https://github.com/hedronvision/bazel-compile-commands-extractor?tab=readme-ov-file#vscode - directions for clangd config
refresh_compile_commands(
    name = "refresh_compile_commands",
    exclude_external_sources = True,  # removed below to have lsp index external cc files at the cost of 2x index time
    # Specify the targets of interest.
    # For example, specify a dict of targets and any flags required to build.
    targets = {
        "//:ray_pkg": "",
    },
    # No need to add flags already in .bazelrc. They're automatically picked up.
)

# bazel run :refresh_compile_commands_external_sources for generation with external source files (cc files)
refresh_compile_commands(
    name = "refresh_compile_commands_external_sources",
    targets = {
        "//:ray_pkg": "",
    },
)

# === Begin of rpc definitions ===
# GRPC common lib.
ray_cc_library(
    name = "grpc_common_base",
    srcs = ["src/ray/rpc/common.cc"],
    hdrs = ["src/ray/rpc/common.h"],
)

ray_cc_library(
    name = "rpc_chaos",
    srcs = ["src/ray/rpc/rpc_chaos.cc"],
    hdrs = ["src/ray/rpc/rpc_chaos.h"],
    deps = [
        "//src/ray/common:ray_config",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/synchronization",
    ],
)

ray_cc_library(
    name = "rpc_client_call",
    hdrs = ["src/ray/rpc/client_call.h"],
    deps = [
        "//src/ray/common:asio",
        "//src/ray/common:grpc_util",
        "//src/ray/common:id",
        "//src/ray/common:status",
        "@com_google_absl//absl/synchronization",
    ],
)

ray_cc_library(
    name = "grpc_client",
    hdrs = ["src/ray/rpc/grpc_client.h"],
    deps = [
        ":grpc_common_base",
        ":rpc_chaos",
        ":rpc_client_call",
        "//src/ray/common:grpc_util",
        "//src/ray/common:ray_config",
        "//src/ray/common:status",
    ],
)

ray_cc_library(
    name = "rpc_server_call",
    srcs = ["src/ray/rpc/server_call.cc"],
    hdrs = ["src/ray/rpc/server_call.h"],
    deps = [
        "//src/ray/common:asio",
        "//src/ray/common:grpc_util",
        "//src/ray/common:id",
        "//src/ray/common:ray_config",
        "//src/ray/common:status",
        "//src/ray/stats:stats_metric",
        "@com_github_grpc_grpc//:grpc++",
    ],
)

ray_cc_library(
    name = "retryable_grpc_client",
    srcs = ["src/ray/rpc/retryable_grpc_client.cc"],
    hdrs = ["src/ray/rpc/retryable_grpc_client.h"],
    deps = [
        ":grpc_client",
        ":rpc_client_call",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/time",
    ],
)

ray_cc_library(
    name = "metrics_agent_client",
    hdrs = ["src/ray/rpc/metrics_agent_client.h"],
    deps = [
        ":grpc_client",
        "//src/ray/protobuf:reporter_cc_proto",
        "//src/ray/util:logging",
        "@com_github_grpc_grpc//:grpc++",
    ],
)

ray_cc_library(
    name = "event_aggregator_client",
    hdrs = ["src/ray/rpc/event_aggregator_client.h"],
    deps = [
        ":grpc_client",
        "//src/ray/protobuf:events_event_aggregator_service_cc_proto",
        "//src/ray/util:logging",
        "@com_github_grpc_grpc//:grpc++",
    ],
)

ray_cc_library(
    name = "grpc_server",
    srcs = ["src/ray/rpc/grpc_server.cc"],
    hdrs = ["src/ray/rpc/grpc_server.h"],
    deps = [
        ":grpc_common_base",
        ":rpc_server_call",
        "//src/ray/common:asio",
        "//src/ray/common:ray_config",
        "//src/ray/common:status",
        "//src/ray/util:thread_utils",
        "@com_github_grpc_grpc//:grpc++",
        "@com_github_grpc_grpc//:grpc++_reflection",
        "@com_github_grpc_grpc//:grpcpp_admin",
    ],
)

ray_cc_library(
    name = "grpc_common_lib",
    # TODO(core): Both two header files are not needed, keep them there because of circular dependency between raylet and node rpc.
    hdrs = [
        "//src/ray/raylet_client:raylet_client.h",
        "//src/ray/raylet_client:raylet_connection.h",
    ],
    # TODO(core): These three dependencies come from raylet client, should be able to remove after we split node rpc and raylet client into smaller targets.
    deps = [
        "//src/ray/common:network",
        "//src/ray/common:ray_object",
        "//src/ray/common:task_common",
    ] + [
        ":event_aggregator_client",
        ":grpc_client",
        ":grpc_common_base",
        ":grpc_server",
        ":metrics_agent_client",
        ":retryable_grpc_client",
        ":rpc_chaos",
        ":rpc_server_call",
    ],
)

# Node manager gRPC lib.
cc_grpc_library(
    name = "node_manager_cc_grpc",
    srcs = ["//src/ray/protobuf:node_manager_proto"],
    grpc_only = True,
    deps = ["//src/ray/protobuf:node_manager_cc_proto"],
)

# Node manager server and client.
ray_cc_library(
    name = "node_manager_rpc",
    srcs = ["src/ray/rpc/node_manager/node_manager_client_pool.cc"],
    hdrs = [
        "src/ray/rpc/node_manager/node_manager_client.h",
        "src/ray/rpc/node_manager/node_manager_client_pool.h",
        "src/ray/rpc/node_manager/node_manager_server.h",
    ],
    deps = [
        ":grpc_common_lib",
        ":node_manager_cc_grpc",
        "//src/ray/common:asio",
        "//src/ray/common:id",
        "//src/ray/common:status",
        "@com_github_grpc_grpc//:grpc++",
    ],
)

# gcs_service gRPC lib.
cc_grpc_library(
    name = "gcs_service_cc_grpc",
    srcs = ["//src/ray/protobuf:gcs_service_proto"],
    grpc_only = True,
    deps = ["//src/ray/protobuf:gcs_service_cc_proto"],
)

# gcs_service gRPC lib.
cc_grpc_library(
    name = "test_service_cc_grpc",
    srcs = ["//src/ray/protobuf:test_service_proto"],
    grpc_only = True,
    deps = ["//src/ray/protobuf:test_service_cc_proto"],
)

# gcs rpc server and client.
ray_cc_library(
    name = "gcs_service_rpc",
    hdrs = [
        "src/ray/rpc/gcs_server/gcs_rpc_client.h",
        "src/ray/rpc/gcs_server/gcs_rpc_server.h",
    ],
    deps = [
        ":autoscaler_cc_grpc",
        ":gcs_service_cc_grpc",
        ":grpc_common_lib",
        "//src/ray/common:asio",
        "//src/ray/common:id",
        "//src/ray/pubsub:pubsub_lib",
        "@boost//:asio",
        "@com_github_grpc_grpc//:grpc++",
        "@com_google_absl//absl/container:btree",
    ],
)

# Object manager gRPC lib.
cc_grpc_library(
    name = "object_manager_cc_grpc",
    srcs = ["//src/ray/protobuf:object_manager_proto"],
    grpc_only = True,
    deps = ["//src/ray/protobuf:object_manager_cc_proto"],
)

# Object manager rpc server and client.
ray_cc_library(
    name = "object_manager_rpc",
    hdrs = [
        "src/ray/rpc/object_manager/object_manager_client.h",
        "src/ray/rpc/object_manager/object_manager_server.h",
    ],
    deps = [
        ":grpc_common_lib",
        ":object_manager_cc_grpc",
        "//src/ray/common:asio",
        "//src/ray/common:status",
        "//src/ray/object_manager:object_manager_grpc_client_manager",
        "@boost//:asio",
        "@com_github_grpc_grpc//:grpc++",
    ],
)

# Worker gRPC lib.
cc_grpc_library(
    name = "worker_cc_grpc",
    srcs = ["//src/ray/protobuf:core_worker_proto"],
    grpc_only = True,
    deps = ["//src/ray/protobuf:worker_cc_proto"],
)

# worker server and client.
ray_cc_library(
    name = "worker_rpc",
    srcs = [
        "src/ray/rpc/worker/core_worker_client.cc",
        "src/ray/rpc/worker/core_worker_client_pool.cc",
    ],
    hdrs = [
        "src/ray/rpc/worker/core_worker_client.h",
        "src/ray/rpc/worker/core_worker_client_pool.h",
        "src/ray/rpc/worker/core_worker_server.h",
    ],
    deps = [
        ":grpc_common_lib",
        ":worker_cc_grpc",
        "//src/ray/common:asio",
        "//src/ray/common:id",
        "//src/ray/gcs/gcs_client:gcs_client_lib",
        "//src/ray/pubsub:pubsub_lib",
        "//src/ray/raylet_client:raylet_client_lib",
        "@com_github_grpc_grpc//:grpc++",
    ],
)

# Metrics Agent gRPC lib.
cc_grpc_library(
    name = "reporter_cc_grpc",
    srcs = ["//src/ray/protobuf:reporter_proto"],
    grpc_only = True,
    deps = ["//src/ray/protobuf:reporter_cc_proto"],
)

# Metrics Agent client.
ray_cc_library(
    name = "reporter_rpc",
    hdrs = [
        "src/ray/rpc/metrics_agent_client.h",
    ],
    deps = [
        ":grpc_common_lib",
        ":reporter_cc_grpc",
        "//src/ray/common:status",
        "@boost//:asio",
        "@boost//:thread",
        "@com_github_grpc_grpc//:grpc++",
    ],
)

# pubsub.
cc_grpc_library(
    name = "pubsub_cc_grpc",
    srcs = ["//src/ray/protobuf:pubsub_proto"],
    grpc_only = True,
    deps = [
        "//src/ray/protobuf:common_cc_proto",
        "//src/ray/protobuf:gcs_cc_proto",
        "//src/ray/protobuf:pubsub_cc_proto",
    ],
)

cc_grpc_library(
    name = "autoscaler_cc_grpc",
    srcs = ["//src/ray/protobuf:autoscaler_proto"],
    grpc_only = True,
    deps = [
        "//src/ray/protobuf:autoscaler_cc_proto",
    ],
)

ray_cc_library(
    name = "autoscaler_rpc",
    deps = [
        ":autoscaler_cc_grpc",
    ],
)

# === End of rpc definitions ===

ray_cc_library(
    name = "ray_mock",
    hdrs = glob(
        ["src/mock/**/*.h"],
        exclude = ["src/mock/ray/common/ray_syncer/ray_syncer.h"],
    ),
)

ray_cc_library(
    name = "ray_mock_syncer",
    hdrs = ["src/mock/ray/common/ray_syncer/ray_syncer.h"],
)

cc_grpc_library(
    name = "ray_syncer_cc_grpc",
    srcs = ["//src/ray/protobuf:ray_syncer_proto"],
    grpc_only = True,
    deps = ["//src/ray/protobuf:ray_syncer_cc_proto"],
)

ray_cc_library(
    name = "ray_common",
    deps = [
        "//src/ray/common:asio",
        "//src/ray/common:constants",
        "//src/ray/common:event_stats",
        "//src/ray/common:file_system_monitor",
        "//src/ray/common:grpc_util",
        "//src/ray/common:id",
        "//src/ray/common:memory_monitor",
        "//src/ray/common:network",
        "//src/ray/common:ray_config",
        "//src/ray/common:ray_syncer",
        "//src/ray/common:status",
        "//src/ray/common:status_or",
        "//src/ray/common:task_common",
        "//src/ray/common:test_util",
        "//src/ray/protobuf:gcs_cc_proto",
        "//src/ray/stats:stats_metric",
        "@com_google_googletest//:gtest",
    ],
)

ray_cc_binary(
    name = "raylet",
    srcs = ["src/ray/raylet/main.cc"],
    visibility = ["//java:__subpackages__"],
    deps = [
        ":raylet_lib",
        "//src/ray/common/cgroup:cgroup_manager",
        "//src/ray/util",
        "//src/ray/util:cmd_line_utils",
        "//src/ray/util:stream_redirection",
        "//src/ray/util:stream_redirection_options",
        "@com_github_gflags_gflags//:gflags",
    ],
)

ray_cc_library(
    name = "pubsub_rpc",
    # TODO(core): Revisit this dependency after grpc_common_lib is broken down into smaller targets.
    deps = [
        ":grpc_common_lib",  # This is a large dependency, should be refined in the future.
        ":pubsub_cc_grpc",
    ],
)

ray_cc_library(
    name = "raylet_agent_manager",
    srcs = ["src/ray/raylet/agent_manager.cc"],
    hdrs = ["src/ray/raylet/agent_manager.h"],
    deps = [
        "//src/ray/common:id",
        "//src/ray/common:ray_config",
        "//src/ray/protobuf:gcs_cc_proto",
        "//src/ray/util",
        "//src/ray/util:event",
        "//src/ray/util:logging",
        "//src/ray/util:process",
        "//src/ray/util:thread_utils",
        "@boost//:asio",
    ],
)

ray_cc_library(
    name = "worker",
    srcs = ["src/ray/raylet/worker.cc"],
    hdrs = ["src/ray/raylet/worker.h"],
    deps = [
        ":node_manager_fbs",
        ":worker_rpc",
        "//src/ray/common:id",
        "//src/ray/common:network",
        "//src/ray/common:task_common",
        "//src/ray/raylet/scheduling:cluster_resource_scheduler",
        "//src/ray/util:process",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/time",
        "@com_google_googletest//:gtest_prod",
    ],
)

ray_cc_library(
    name = "runtime_env_agent_client",
    srcs = ["src/ray/raylet/runtime_env_agent_client.cc"],
    hdrs = ["src/ray/raylet/runtime_env_agent_client.h"],
    deps = [
        "//src/ray/common:asio",
        "//src/ray/common:id",
        "//src/ray/common:ray_config",
        "//src/ray/common:status",
        "//src/ray/protobuf:gcs_cc_proto",
        "//src/ray/protobuf:runtime_env_agent_cc_proto",
        "//src/ray/util:logging",
        "@boost//:beast",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/strings:str_format",
    ],
)

ray_cc_library(
    name = "worker_pool",
    srcs = ["src/ray/raylet/worker_pool.cc"],
    hdrs = ["src/ray/raylet/worker_pool.h"],
    deps = [
        ":runtime_env_agent_client",
        ":worker",
        "//src/ray/common:constants",
        "//src/ray/common:network",
        "//src/ray/common:ray_config",
        "//src/ray/common:runtime_env",
        "//src/ray/common:status",
        "//src/ray/common:task_common",
        "//src/ray/core_worker:core_worker_common",
        "//src/ray/gcs/gcs_client:gcs_client_lib",
        "@boost//:system",
        "@com_google_absl//absl/strings",
    ],
)

ray_cc_library(
    name = "wait_manager",
    srcs = ["src/ray/raylet/wait_manager.cc"],
    hdrs = ["src/ray/raylet/wait_manager.h"],
    deps = [
        "//src/ray/common:id",
        "//src/ray/util:container_util",
    ],
)

ray_cc_library(
    name ="local_object_manager_interface",
    hdrs = ["src/ray/raylet/local_object_manager_interface.h"],
    deps = [
        "//src/ray/common:id",
        "//src/ray/common:ray_object",
        "//src/ray/protobuf:node_manager_cc_proto",
    ],
)

ray_cc_library(
    name = "local_object_manager",
    srcs = ["src/ray/raylet/local_object_manager.cc"],
    hdrs = ["src/ray/raylet/local_object_manager.h"],
    deps = [
        ":local_object_manager_interface",
        ":worker_pool",
        ":worker_rpc",
        "//src/ray/common:id",
        "//src/ray/common:ray_object",
        "//src/ray/gcs/gcs_client:gcs_client_lib",
        "//src/ray/object_manager:object_directory",
        "//src/ray/object_manager:object_manager_common",
        "//src/ray/protobuf:node_manager_cc_proto",
        "//src/ray/pubsub:subscriber_lib",
    ],
)

ray_cc_library(
    name = "raylet_lib",
    srcs = [
        "src/ray/raylet/dependency_manager.cc",
        "src/ray/raylet/local_task_manager.cc",
        "src/ray/raylet/node_manager.cc",
        "src/ray/raylet/placement_group_resource_manager.cc",
        "src/ray/raylet/raylet.cc",
        "src/ray/raylet/worker_killing_policy.cc",
        "src/ray/raylet/worker_killing_policy_group_by_owner.cc",
        "src/ray/raylet/worker_killing_policy_retriable_fifo.cc",
    ],
    hdrs = [
        "src/ray/raylet/dependency_manager.h",
        "src/ray/raylet/local_task_manager.h",
        "src/ray/raylet/node_manager.h",
        "src/ray/raylet/placement_group_resource_manager.h",
        "src/ray/raylet/raylet.h",
        "src/ray/raylet/test/util.h",
        "src/ray/raylet/worker_killing_policy.h",
        "src/ray/raylet/worker_killing_policy_group_by_owner.h",
        "src/ray/raylet/worker_killing_policy_retriable_fifo.h",
    ],
    linkopts = select({
        "@platforms//os:windows": [
        ],
        "//conditions:default": [
            "-lpthread",
        ],
    }),
    deps = [
        ":local_object_manager_interface",
        ":local_object_manager",
        ":node_manager_fbs",
        ":node_manager_rpc",
        ":raylet_agent_manager",
        ":runtime_env_agent_client",
        ":wait_manager",
        ":worker",
        ":worker_pool",
        ":worker_rpc",
        "//src/ray/common:memory_monitor",
        "//src/ray/core_worker:experimental_mutable_object_provider",
        "//src/ray/gcs",
        "//src/ray/gcs/gcs_client:gcs_client_lib",
        "//src/ray/object_manager",
        "//src/ray/object_manager:ownership_object_directory",
        "//src/ray/object_manager/plasma:plasma_client",
        "//src/ray/protobuf:common_cc_proto",
        "//src/ray/protobuf:runtime_env_agent_cc_proto",
        "//src/ray/pubsub:pubsub_lib",
        "//src/ray/raylet/scheduling:scheduler",
        "//src/ray/stats:stats_lib",
        "//src/ray/util:cmd_line_utils",
        "//src/ray/util:container_util",
        "//src/ray/util:throttler",
        "@boost//:asio",
        "@boost//:system",
        "@com_github_jupp0r_prometheus_cpp//pull",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_googletest//:gtest_prod",
        "@io_opencensus_cpp//opencensus/exporters/stats/prometheus:prometheus_exporter",
        "@io_opencensus_cpp//opencensus/stats",
        "@io_opencensus_cpp//opencensus/tags",
    ],
)

# This header is used to wrap some internal codes so we can reduce suspicious
# symbols export.
ray_cc_library(
    name = "exported_internal",
    srcs =
        [
            "src/ray/internal/internal.cc",
        ],
    hdrs =
        [
            "src/ray/internal/internal.h",
        ],
    copts = COPTS,
    strip_include_prefix = "src",
    deps = [
        "//src/ray/core_worker:core_worker_common",
        "//src/ray/core_worker:core_worker_lib",
    ],
    alwayslink = 1,
)

ray_cc_test(
    name = "local_object_manager_test",
    size = "small",
    srcs = [
        "src/ray/raylet/test/local_object_manager_test.cc",
    ],
    tags = ["team:core"],
    deps = [
        ":ray_mock",
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "worker_pool_test",
    size = "small",
    srcs = ["src/ray/raylet/worker_pool_test.cc"],
    tags = [
        "no_tsan",
        "team:core",
    ],
    deps = [
        ":ray_mock",
        ":worker_pool",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "placement_group_resource_manager_test",
    size = "small",
    srcs = ["src/ray/raylet/placement_group_resource_manager_test.cc"],
    tags = ["team:core"],
    deps = [
        "ray_common",
        "raylet_lib",
        ":ray_mock",
        "//src/ray/gcs/test:gcs_test_util_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "runtime_env_agent_client_test",
    size = "small",
    srcs = ["src/ray/raylet/runtime_env_agent_client_test.cc"],
    tags = ["team:core"],
    deps = [
        "ray_common",
        "raylet_lib",
        ":ray_mock",
        "//src/ray/gcs/test:gcs_test_util_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "dependency_manager_test",
    size = "small",
    srcs = ["src/ray/raylet/dependency_manager_test.cc"],
    tags = ["team:core"],
    deps = [
        ":raylet_lib",
        "//:ray_mock",
        "//src/ray/common:test_util",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "local_task_manager_test",
    size = "small",
    srcs = ["src/ray/raylet/local_task_manager_test.cc"],
    tags = ["team:core"],
    deps = [
        ":ray_mock",
        ":raylet_lib",
        "//src/ray/common:test_util",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "node_manager_test",
    size = "small",
    srcs = ["src/ray/raylet/test/node_manager_test.cc"],
    tags = ["team:core"],
    deps = [
        ":ray_mock",
        ":local_object_manager_interface",
        "//src/ray/util:macros",
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "wait_manager_test",
    size = "small",
    srcs = ["src/ray/raylet/wait_manager_test.cc"],
    tags = ["team:core"],
    deps = [
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "worker_killing_policy_test",
    size = "small",
    srcs = [
        "src/ray/raylet/worker_killing_policy_test.cc",
    ],
    tags = ["team:core"],
    deps = [
        ":ray_common",
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "worker_killing_policy_group_by_owner_test",
    size = "small",
    srcs = [
        "src/ray/raylet/worker_killing_policy_group_by_owner_test.cc",
    ],
    tags = ["team:core"],
    deps = [
        ":ray_common",
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "worker_killing_policy_retriable_fifo_test",
    size = "small",
    srcs = [
        "src/ray/raylet/worker_killing_policy_retriable_fifo_test.cc",
    ],
    tags = ["team:core"],
    deps = [
        ":ray_common",
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "grpc_server_client_test",
    size = "small",
    srcs = [
        "src/ray/rpc/test/grpc_server_client_test.cc",
    ],
    tags = ["team:core"],
    deps = [
        ":grpc_common_lib",
        ":test_service_cc_grpc",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "rpc_chaos_test",
    size = "small",
    srcs = [
        "src/ray/rpc/test/rpc_chaos_test.cc",
    ],
    tags = ["team:core"],
    deps = [
        ":grpc_common_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_test(
    name = "core_worker_client_pool_test",
    size = "small",
    srcs = [
        "src/ray/rpc/worker/test/core_worker_client_pool_test.cc",
    ],
    tags = ["team:core"],
    deps = [
        ":ray_mock",
        ":worker_rpc",
        "@com_google_googletest//:gtest_main",
    ],
)

ray_cc_library(
    name = "platform_shims",
    srcs = [] + select({
        "@platforms//os:windows": glob([
            "src/shims/windows/**/*.c",
            "src/shims/windows/**/*.cc",
            "src/shims/windows/**/*.h",
        ]),
        "//conditions:default": [],
    }),
    hdrs = [] + select({
        "@platforms//os:windows": glob([
            "src/shims/windows/**/*.h",
        ]),
        "//conditions:default": [],
    }),
    strip_include_prefix = select({
        "@platforms//os:windows": "src/shims/windows",
        "//conditions:default": "",
    }),
)

filegroup(
    name = "extra_actions_base_proto",
    srcs = [
        "thirdparty/protobuf/extra_actions_base.proto",
    ],
)

proto_library(
    name = "extra_actions_base_proto_lib",
    srcs = ["thirdparty/protobuf/extra_actions_base.proto"],
)

cc_proto_library(
    name = "extra_actions_cc_proto_lib",
    deps = [":extra_actions_base_proto_lib"],
)

alias(
    name = "hiredis",
    actual = "@com_github_redis_hiredis//:hiredis",
)

flatbuffer_cc_library(
    name = "node_manager_fbs",
    srcs = ["src/ray/raylet/format/node_manager.fbs"],
    flatc_args = FLATC_ARGS,
    out_prefix = "ray/raylet/format/",
)

pyx_library(
    name = "_raylet",
    srcs = glob([
        "python/ray/__init__.py",
        "python/ray/_raylet.pxd",
        "python/ray/_raylet.pyx",
        "python/ray/includes/*.pxd",
        "python/ray/includes/*.pxi",
    ]),
    # Export ray ABI symbols, which can then be used by _streaming.so.
    # We need to dlopen this lib with RTLD_GLOBAL to use ABI in this
    # shared lib, see python/ray/__init__.py.
    cc_kwargs = dict(
        srcs = PYX_SRCS,
        # cython code is auto-generated, which is out of our control.
        copts = COPTS + PYX_COPTS,
        # This is needed since Windows has long file path issues and command length limits
        features = ["compiler_param_file"],
        # see https://github.com/tensorflow/tensorflow/blob/r2.1/tensorflow/lite/BUILD#L444
        linkopts = select({
            "@platforms//os:osx": [
                "-Wl,-exported_symbols_list,$(location //:src/ray/ray_exported_symbols.lds)",
            ],
            "@platforms//os:windows": [],
            "//conditions:default": [
                "-Wl,--version-script,$(location //:src/ray/ray_version_script.lds)",
            ],
        }),
        linkstatic = 1,
    ),
    deps = [
        "//:exported_internal",
        "//:raylet_lib",
        "//:src/ray/ray_exported_symbols.lds",
        "//:src/ray/ray_version_script.lds",
        "//src/ray/core_worker:core_worker_lib",
        "//src/ray/gcs:gcs_redis_client",
        "//src/ray/gcs/gcs_client:gcs_python_callbacks",
        "//src/ray/gcs/gcs_client:global_state_accessor_lib",
        "//src/ray/gcs/gcs_server:gcs_server_lib",
        "//src/ray/protobuf:serialization_cc_proto",
        "//src/ray/thirdparty/setproctitle",
        "//src/ray/util",
        "//src/ray/util:memory",
        "//src/ray/util:stream_redirection",
        "//src/ray/util:stream_redirection_options",
    ],
)

filegroup(
    name = "python_sources",
    srcs = glob([
        "python/ray/*.py",
        "python/ray/autoscaler/*.py",
        "python/ray/autoscaler/_private/*.py",
        "python/ray/autoscaler/_private/_azure/*.json",
        "python/ray/autoscaler/aws/defaults.yaml",
        "python/ray/autoscaler/azure/defaults.yaml",
        "python/ray/autoscaler/gcp/defaults.yaml",
        "python/ray/autoscaler/local/defaults.yaml",
        "python/ray/autoscaler/vsphere/defaults.yaml",
        "python/ray/cloudpickle/*.py",
        "python/ray/core/__init__.py",
        "python/ray/core/generated/__init__.py",
        "python/ray/core/generated/ray/__init__.py",
        "python/ray/core/generated/ray/protocol/__init__.py",
        "python/ray/dashboard/**/*.py",
        "python/ray/experimental/*.py",
        "python/ray/util/*.py",
        "python/ray/internal/*.py",
        "python/ray/workers/default_worker.py",
    ]),
)

alias(
    name = "redis-server",
    actual = select({
        "@platforms//os:windows": "@com_github_tporadowski_redis_bin//:redis-server.exe",
        "//conditions:default": "@com_github_antirez_redis//:redis-server",
    }),
)

alias(
    name = "redis-cli",
    actual = select({
        "@platforms//os:windows": "@com_github_tporadowski_redis_bin//:redis-cli.exe",
        "//conditions:default": "@com_github_antirez_redis//:redis-cli",
    }),
)

filegroup(
    name = "all_py_proto",
    srcs = [
        "//src/ray/protobuf:autoscaler_py_proto",
        "//src/ray/protobuf:common_py_proto",
        "//src/ray/protobuf:core_worker_py_proto",
        "//src/ray/protobuf:event_py_proto",
        "//src/ray/protobuf:events_event_aggregator_service_py_proto",
        "//src/ray/protobuf:export_event_py_proto",
        "//src/ray/protobuf:gcs_py_proto",
        "//src/ray/protobuf:gcs_service_py_proto",
        "//src/ray/protobuf:instance_manager_py_proto",
        "//src/ray/protobuf:metrics_service_py_proto",
        "//src/ray/protobuf:node_manager_py_proto",
        "//src/ray/protobuf:ray_client_py_proto",
        "//src/ray/protobuf:reporter_py_proto",
        "//src/ray/protobuf:runtime_env_agent_py_proto",
        "//src/ray/protobuf:runtime_env_common_py_proto",
        "//src/ray/protobuf:usage_py_proto",
    ],
)

filegroup(
    name = "serve_py_proto",
    srcs = [
        "//src/ray/protobuf:serve_py_proto",
    ],
)

# This is a dummy test dependency that causes the python tests to be
# re-run if any of these files changes.
py_library(
    name = "ray_lib",
    srcs = glob(
        ["python/ray/**/*.py"],
        exclude = ["python/ray/tests/*.py"],
    ),
    visibility = ["__subpackages__"],
)

copy_to_workspace(
    name = "cp_raylet_so",
    srcs = ["python/ray/_raylet.so"],
    dstdir = "python/ray",
)

copy_to_workspace(
    name = "cp_all_py_proto",
    srcs = [":all_py_proto"],
    dstdir = "python/ray/core/generated",
)

copy_to_workspace(
    name = "cp_serve_py_proto",
    srcs = [":serve_py_proto"],
    dstdir = "python/ray/serve/generated",
)

copy_to_workspace(
    name = "cp_redis",
    srcs = [
        ":redis-cli",
        ":redis-server",
    ],
    dstdir = "python/ray/core/src/ray/thirdparty/redis/src",
)

copy_to_workspace(
    name = "cp_raylet",
    srcs = [":raylet"],
    dstdir = "python/ray/core/src/ray/raylet",
)

copy_to_workspace(
    name = "cp_gcs_server",
    srcs = ["//src/ray/gcs/gcs_server"],
    dstdir = "python/ray/core/src/ray/gcs",
)

copy_to_workspace(
    name = "cp_jemalloc",
    srcs = ["@jemalloc//:shared"],
    dstdir = "python/ray/core/",
)

genrule(
    name = "install_py_proto",
    srcs = [
        ":cp_all_py_proto",
        ":cp_serve_py_proto",
    ],
    outs = ["install_py_proto.out"],
    cmd = """
        # NOTE(hchen): Protobuf doesn't allow specifying Python package name. So we use this `sed`
        # command to change the import path in the generated file.
        # shellcheck disable=SC2006
        files=(
            `ls python/ray/core/generated/*_pb2*.py` \
            `ls python/ray/serve/generated/*_pb2*.py` \
            )
        sed -i -E 's/from src.ray.protobuf/from ./' "$${files[@]}"
        # Help the generated serve files to have the correct module
        serve_files=(`ls python/ray/serve/generated/*_pb2*.py`)
        sed -i -E 's/'"'"'src.ray.protobuf./'"'"'ray.serve.generated./' "$${serve_files[@]}"
        # TODO(sang): Build our own proto instead of creating a new proto for opencensus separately.
        # https://github.com/ray-project/ray/issues/31358
        sed -i -E 's/from opencensus.proto.metrics.v1 import/from . import/' "$${files[@]}"
        sed -i -E 's/from opencensus.proto.resource.v1 import/from . import/' "$${files[@]}"
        echo "$${PWD}" > $@
    """,
    local = 1,
)

genrule(
    name = "ray_pkg",
    srcs = [
        ":cp_raylet_so",
        ":python_sources",
        ":install_py_proto",
        ":cp_redis",
        ":cp_raylet",
        ":cp_gcs_server",
    ] + select({
        ":jemalloc": [
            ":cp_jemalloc",
        ],
        "//conditions:default": [],
    }),
    outs = ["ray_pkg.out"],
    cmd = """
        if [ "$${OSTYPE-}" = "msys" ]; then
            ln -P -f -- python/ray/_raylet.so python/ray/_raylet.pyd
        fi
        echo "$${PWD}" > $@
    """,
    local = 1,
)
